import "./index.css"

function SubmitForm() {


    var inputLis = document.querySelectorAll('input');

    var username = inputLis[0].value;
    var password = inputLis[1].value;

    // console.log(username);
    // console.log(password);
    var xhr = new XMLHttpRequest();

    xhr.open("get", "http://127.0.0.1:5500/#list", true);

    xhr.send();

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            let text = xhr.responseText;
            console.log(text);
        }
    }
    var data = {
        username: '04191003',
        password: '20011127'
    }

    if (username == data.username) {

        if (password == data.password) {

            alert("登录成功！");
            window.parent.$router.push('pc/get-applicant-infrom');
        } else {

            alert("密码错误，登录失败！");
        }

    } else {

        alert("用户名不存在，登录失败！");
    }
}

var btn = document.querySelector('button');
btn.addEventListener('click', function() {
    SubmitForm();
});

var username = document.querySelector('#username');
var password = document.querySelector('#password');

function checkNum(obj) {
    var num = obj.value;

    if (num.match(/[0-9]{8}$/) == null || num.length != 8) {
        return false;
    }
    return true;
}

function checkCode(obj) {
    var code = obj.value;

    if (code.match(/[\s\S*]{6,16}$/) == null || code.length > 16) {
        return false;
    }
    return true;
}

username.addEventListener("keydown", function(e) {
    // 按下回车键且输入框值非空时
    if (e.keyCode == 13 && username.value) {
        password.focus();
    }
})

username.addEventListener("blur", function() {
    var usernameBox = document.querySelector('#usernameBox');
    if (!checkNum(username)) {
        username.style.borderColor = 'red';
        if (username.value == '') {
            usernameBox.innerText = "学号不能为空！";
        } else {
            usernameBox.innerText = "请输入正确的学号！";
        }
    } else {
        username.style.borderColor = ' #c8cccf';
        usernameBox.innerText = "";
    }
})

password.addEventListener("keydown", function(e) {
    // 按下回车键且输入框值非空时
    if (e.keyCode == 13 && password.value) {
        btn.click();
    }
})

password.addEventListener("blur", function() {
    var passwordBox = document.querySelector('#passwordBox');
    if (!checkCode(password)) {
        password.style.borderColor = 'red';
        if (password.value == '') {
            passwordBox.innerText = "密码不能为空！";
        } else {
            passwordBox.innerText = "请输入6-16位任意字符！";
        }
    } else {
        password.style.borderColor = ' #c8cccf';
        passwordBox.innerText = "";
    }
})